<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="途经点管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="600px"
    >
        <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="站点" prop="stationId">
                <el-select
                    v-model="form.stationId"
                    filterable
                    placeholder="请选择"
                    @change="selectStation()"
                >
                    <el-option
                        v-for="item in stationOptions"
                        :key="item.id"
                        :label="item.stationName"
                        :value="item.id"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="车场" prop="carParkId">
                <el-select
                    v-model="form.carParkId"
                    filterable
                    placeholder="请选择"
                >
                    <el-option
                        v-for="item in carParkOptions"
                        :key="item.id"
                        :label="item.carParkName"
                        :value="item.id"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="时间" prop="fromStartStationTime">
                <el-input-number v-model="form.fromStartStationTime" controls-position="right" :min="0"/>
                <div class="form-tips">离第一出发地点时间（分钟）</div>
            </el-form-item>

            <el-form-item label="距离" prop="fromStartStationDistance">
                <el-input-number v-model="form.fromStartStationDistance" controls-position="right" :min="0"/>
                <div class="form-tips">离第一出发地点距离（km）</div>
            </el-form-item>

            <el-form-item label="出发顺序" prop="sort">
                <el-input-number v-model="form.sort" :min="1" controls-position="right"/>
            </el-form-item>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark"/>
            </el-form-item>

            <el-form-item>
                <el-popconfirm :title="'确定保存途经点吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {StationApi} from '@/api/intercity/StationApi'
import {LineViaApi} from '@/api/intercity/LineViaApi'
import {CarParkApi} from '@/api/intercity/CarParkApi'
import {Common} from '@/utils/common'

export default {
    name: 'LineViaUpdate',
    components: {},
    data() {
        return {
            form: {
                id: 0,
                lineId: 0,
                stationId: '',
                carParkId: '',
                fromStartStationTime: 0,
                remark: '',
                sort: 0
            },
            rules: {
                stationId: [
                    {required: true, message: '请选择站点', trigger: 'change'}
                ],
                carParkId: [
                    {required: true, message: '请选择车场', trigger: 'change'}
                ],
                fromStartStationTime: [
                    {required: true, message: '请输入离第一出发地点时间（分钟）', trigger: 'blur'}
                ],
                sort: [
                    {required: true, message: '请输入排序', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            stationOptions: [],
            carParkOptions: []
        }
    },
    methods: {
        // 显示组件
        showUpdate(data, lineId) {
            const that = this
            that.dialogVisible = true
            if (that.$refs['formRef'] !== undefined) {
                that.$refs['formRef'].resetFields()
            }

            that.$nextTick(() => {
                this.getStationList()
                if (data.id > 0) {
                    this.form = Common.assign(this.form, data)
                    if (data.carParkId > 0) {
                        this.getCarParkList()
                    }
                }else{
                    this.form.id = 0
                }
            })

            this.form.lineId = lineId
        },

        // 获取站点列表
        getStationList() {
            StationApi.getList().then((data) => {
                this.stationOptions = data
            })
        },

        // 获取站点列表
        getCarParkList() {
            const sid = this.form.stationId === '' ? 0 : this.form.stationId
            CarParkApi.getList(sid).then((data) => {
                this.carParkOptions = data
            })
        },

        // 选择站点
        selectStation() {
            this.form.carParkId = ''
            this.getCarParkList()
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    let data = Common.deepClone(that.form)
                    data.fromStartStationDistance = data.fromStartStationDistance * 1000
                    if (data.id > 0) {
                        LineViaApi.edit(data).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    } else {
                        LineViaApi.create(data).then((response) => {
                            that.dialogVisible = false
                            that.$emit('refresh')
                        })
                    }
                }
            })
        }
    }
}
</script>
